
package jz.service.xqdy;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import SoftFan.util.textUnit;
import SoftFan.workflow.protocol.db.wf_Db_Connect;
import SoftFan.workflow.protocol.db.wf_Db_Exception;

public class XqdyMgr {

	private static class SingletonHolder {

		private static XqdyMgr	INSTANCE	= new XqdyMgr();
	}

	public static XqdyMgr getInstance() {
		return SingletonHolder.INSTANCE;
	}

	public jz.dbUnit.xqdyqd.gjzXqdyqd build(wf_Db_Connect dbconnect, long id) throws wf_Db_Exception {
		jz.dbUnit.xqdyph.gjzXpdyph ph = null;
		{
			List<jz.dbUnit.xqdyph.gjzXpdyph> phs = jz.dbUnit.xqdyph.gjzXpdyphJDBCUnit.getInstance().load(dbconnect, (String) null, "xqyear desc,xqpc desc", 1);
			if (phs.size() > 0) {
				ph = phs.remove(0);
				jz.dbUnit.xqdyph.gjzXpdyphJDBCUnit.getInstance().freeData(phs);
			}
		}
		Calendar d = Calendar.getInstance();
		d.setTime(new Date(System.currentTimeMillis()));
		int year = d.get(Calendar.YEAR);
		if (ph != null) {
			if (ph.getXqyear() != year) {
				ph.freeData();
				ph = null;
			}
		}
		if (ph == null) {
			ph = new jz.dbUnit.xqdyph.gjzXpdyph();
			ph.setXqyear(year);
			ph.setXqpc(1);
			ph.setXqidx(1);
			ph.setId(Integer.toString(ph.getXqyear()).substring(2) + textUnit.formatLenString(Integer.toString(ph.getXqpc()), 3));
			jz.dbUnit.xqdyph.gjzXpdyphJDBCUnit.getInstance().Create(dbconnect, ph);
		} else {
			int idx = ph.getXqidx() + 1;
			if (idx > 99) {
				ph.setXqpc(ph.getXqpc() + 1);
//				ph.setId(Integer.toString(ph.getXqyear()).substring(2) + textUnit.formatLenString(Integer.toString(ph.getXqpc()), 3));
				ph.setId((Integer.parseInt(ph.getId()) + 1)+"");
				ph.setXqidx(1);
				jz.dbUnit.xqdyph.gjzXpdyphJDBCUnit.getInstance().Create(dbconnect, ph);
			} else {
				ph.setXqidx(idx);
				jz.dbUnit.xqdyph.gjzXpdyphJDBCUnit.getInstance().UpdateField(dbconnect, ph, "xqidx;");
			}
		}
		
		jz.dbUnit.xqdyqd.gjzXqdyqd gjzxq = new jz.dbUnit.xqdyqd.gjzXqdyqd();
		gjzxq.setId(ph.getId());
		gjzxq.setGjzid(id);
		gjzxq.setXqdate(d.getTime());
		gjzxq.setCidx(ph.getXqidx());
		jz.dbUnit.xqdyqd.gjzXqdyqdJDBCUnit.getInstance().Save(dbconnect, gjzxq);
		
		return gjzxq;
	}
	
	public static void main(String[] args) {
		System.out.println((Integer.parseInt("18003") + 1));
	}
}
